<?php
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet 
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2011 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <xinqiyang@gmail.com>
// +----------------------------------------------------------------------

class RelationService extends  BaseService
{
	
	public static function focus($uid,$id)
	{
		$where = "modela_id=$uid and modelb_id=$id";
		$r = self::get('modelandmodel','','',$where);
		if(!$r){
			return self::relationAdd($uid, $id,$uid,'focus');
		}elseif(isset($r['status']) && $r['status'] ==1 ){
			$r['status'] = 0;
			return self::save('modelandmodel', $r);
		}
		//@TODO:LOG && RECORD TO EVENT 
		return false;
	}
	
	public static function setUserRelation($array)
	{
		extract($array);
		$where = "modela_id=$uid and modelb_id=$id and type='$atype' and status=0";
		$r = self::get('modelandmodel','','',$where);
		if(!$r){
			return self::relationAdd($uid, $id,$uid,$atype);
		}elseif(isset($r['status']) && $r['status'] ==1 ){
			$r['status'] = 0;
			return self::save('modelandmodel', $r);
		}
		//@TODO:LOG && RECORD TO EVENT 
		return false;
	}
	
	public static function unfocus($uid,$id)
	{
		$where = "modela_id=$uid and modelb_id=$id and status=0";
		$r = self::get('modelandmodel','','',$where);
		if($r){
			//save
			$r['status'] = 1;
			return self::save('modelandmodel', $r);
		}
		//@TODO:LOG && RECORD TO EVENT 
		return false;
	}
	
	
	public static function getUserRelation($uid,$id,$type,$limit)
	{
		$where = "user_id=$uid and modela_id=$id and status=0 and type='$type'";
		$r = self::gets('modelandmodel', $where);
		if(!$r)
		{
			return false;
		}
		return $r;
	}
	
	public static function getRelation($id,$type,$limit)
	{
		$where = "modela_id=$id and status=0 and type='$type'";
		$r = self::gets('modelandmodel', $where);
		if(!$r)
		{
			return false;
		}
		return $r;
	}
	
	
	/**
	 * add the relation id
	 * Enter description here ...
	 * @param unknown_type $modelaid
	 * @param unknown_type $modelbid
	 * @param unknown_type $userid
	 * @param unknown_type $type
	 * @param unknown_type $locationid
	 * @param unknown_type $extention
	 */
	private static function relationAdd($modelaid,$modelbid,$userid,$type,$locationid='',$extention='')
	{
		$behavior = C('behavior');
		if(in_array($type,$behavior)){
			$data['id']= objid();
			$data['status']='0';
			$data['modela_id']= $modelaid;
			$data['modelb_id']= $modelbid;
			$data['createtime']= time();
			$data['user_id']= $userid;
			$data['type']= $type; // define type 
			$data['locationid']= $locationid;
			$data['extension']= $extention;
			return self::add('modelandmodel', $data);
		}
		logWARNING(__CLASS__.'/'.__FUNCTION__.":$userid post the undefined behavior");
		return false;
	}
	
}